<?php
/**
 * Created by PhpStorm.
 * User: yangyou
 * Date: 2018/12/3 0003
 * Time: 10:10
 * 施工案例
 */

namespace app\index\controller;


use app\index\model\User;

class Msg extends Base
{

    //用户咨询
    public function userConsultation(){

        //获取用户信息
        $user_info = session('user_info');
        if(!empty($user_info["user_id"])) {
            $ImgApiData = config("ImgApiData");
            $result = file_get_contents($ImgApiData["domain"] . url($ImgApiData["ApiList"]["getRoom"], [
                    'token' => md5(md5($ImgApiData["key"]) . date("Y-m-d")),
                    "uid" => 'user_' . $user_info["user_id"],
            ]));
            $result = json_decode($result, true);
            if ($result["code"] == 0) {
                $adminInfo = $result["data"];
                $adminInfo["nick_name"] = mb_substr($adminInfo["nick_name"], 0, 1, 'utf-8') . "工";
                $this->assign("adminInfo", $adminInfo);
                //聊天服务器登录设置
                $this->assign("uid", [
                    "uid" => 'user_' . $user_info["user_id"],
                    "name" => urlencode($user_info["nick_name"]),
                    "room" => $adminInfo["room"],
                    "map" => $adminInfo["map"],
                    "thumb" => urlencode("//tva1.sinaimg.cn/crop.0.0.118.118.180/5db11ff4gw1e77d3nqrv8j203b03cweg.jpg"),
                ]);
                $this->assign("userInfo", [
                    "uid" => 'user_' . $user_info["user_id"],
                    "name" => '我',
                    "thumb" => "//tva1.sinaimg.cn/crop.0.0.118.118.180/5db11ff4gw1e77d3nqrv8j203b03cweg.jpg",
                ]);
            } else {
                $this->success($result["msg"], '/');
            }
        }else{
            $this->success("请先登录！",'/');
        }
        return view('msg/userConsultation',[
            "title" =>  "客服咨询",
        ]);
    }

    //回复用户聊天
    public function sendChatMsg(){
        if(request()->isPost()){
            $data = input("post.");
            $User = new User();
            $msg = $User->checkData($data,[
                ["room_id", "require", "发送房间号不能为空"],
                ["user_id", "require", "用户id不能为空"],
                ["content", "require", "回复内容不能为空"],
            ]);
            if($msg === true){
                $data = $this->sendChat([
                    "type"      =>  "sendChatMsg",
                    "room_id"   =>  $data['room_id'],
                    "user_id"   =>  $data['user_id'],
                    "token"     =>  md5(md5(config('socketDate')).date('Y-m-d')),
                    "content"   =>  $data['content'],
                    "time"      =>  date("Y-m-d H:i:s"),
                ]);
                $result = json_decode($data,true);
            }else{
                $result['code'] = 1;
                $result['msg'] = "回复内容不能为空！";
            }
        }else{
            $result['code'] = 1;
            $result['msg'] = "请求错误！";
        }
        return json($result);
    }

    //发送消息
    private function sendChat($data){
        $push_api_url = "http://".config("msgDomain").":2121/";
        //初始化
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $push_api_url);
        //设置头文件的信息作为数据流输出
        //curl_setopt($curl, CURLOPT_HEADER, 1);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //curl_setopt($curl, CURLOPT_TIMEOUT, 1);
        //设置请求头
        //curl_setopt($curl, CURLOPT_HTTPHEADER, $this->header);
        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);
        //设置post数据
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        //显示获得的数据
        return $data;

    }


}